﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace RunVistaGadget31
{
 /// <summary>
 /// 
 /// </summary>
    public partial class GadgetSettings : Form
    {
        private GadgetProperties GadgetProp;
        
        /// <summary>
        /// 
        /// </summary>
        public GadgetSettings()
        {
            InitializeComponent();
        }

        private void btnCancelSaveGadgetSettings_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void btnSaveGadgetSettings_Click(object sender, EventArgs e)
        {
            SaveSettings();
            this.Close();
        }

        private void SaveSettings()
        {
            if (GadgetProp == null)
            {
                GadgetProp = new GadgetProperties();
            }
            SaveShortcutForRunGadget();
            SaveGadgetOutputPath();
            SaveArchivatorPath();
            SaveIsBuildSignedGadget();
            SaveAllowedFileExtensions();
            if (GadgetProp.IsBuildSignedGadget)
            {
                SaveSigntoolPath();
                SavePfxFilePath();
                SavePfxPwdPath();
            }

        }

        private void SaveShortcutForRunGadget()
        {
            string NewShortcut = this.txtShortcutForRunGadget.Text;
            if (!String.IsNullOrEmpty(GadgetProp.ShortcutForRunGadget) && GadgetProp.ShortcutForRunGadget != NewShortcut)
            {
                Form dlg1 = CreateDialogForm(String.Format("You are alredy have shortcut:'{0}' that is associated with this command. Do you want to rewrite it?", GadgetProp.ShortcutForRunGadget));
                if (dlg1.ShowDialog() == DialogResult.OK)
                {
                    GadgetProp.ShortcutForRunGadget = this.txtShortcutForRunGadget.Text;
                }
                dlg1.Close();
            }
        }

        private void SaveIsBuildSignedGadget()
        {
            GadgetProp.IsBuildSignedGadget = this.chkIsBuildSignedGadget.Checked;
        }

        private void SaveGadgetOutputPath()
        {
            GadgetProp.GadgetOutputPath = this.txtOutputGadgetPath.Text;
        }

        private void SaveArchivatorPath()
        {
            GadgetProp.ArchivatorPath = this.txtCabarcPath.Text;
        }

        private void SaveSigntoolPath()
        {
            GadgetProp.SigntoolPath = this.txtSigntoolPath.Text;
        }

        private void SavePfxFilePath()
        {
            GadgetProp.PfxFilePath = this.txtPfxFilePath.Text;
        }

        private void SavePfxPwdPath()
        {
            GadgetProp.PfxPassword = this.txtPrivateKeyPassword.Text;
        }

        private void SaveAllowedFileExtensions()
        {
            GadgetProp.AllowedExtensionsForGadget = this.txtAllowedFileExtension.Text;
        }

        private void LoadGadgetSettings(object sender, EventArgs e)
        {
            LoadSettings();
        }

        private void LoadSettings()
        {
            if (GadgetProp == null)
            {
                GadgetProp = new GadgetProperties();
            }
            LoadShortcutRunGadget();
            LoadGadgetOutputPath();
            LoadArchivatorPath();
            LoadIsBuildSignedGadget();
            LoadSigntoolPath();
            LoadPfxFilePath();
            LoadPfxPwdPath();
            LoadAllowedFileExtensions();
        }

        private void LoadShortcutRunGadget()
        {
            if (!String.IsNullOrEmpty(GadgetProp.ShortcutForRunGadget))
            {
                this.txtShortcutForRunGadget.Text = GadgetProp.ShortcutForRunGadget;
            }

        }

        private void LoadIsBuildSignedGadget()
        {
            this.chkIsBuildSignedGadget.Checked = GadgetProp.IsBuildSignedGadget;
        }

        private void LoadGadgetOutputPath()
        {
            if (!String.IsNullOrEmpty(GadgetProp.GadgetOutputPath))
            {
                this.txtOutputGadgetPath.Text = GadgetProp.GadgetOutputPath;
            }
        }

        private void LoadArchivatorPath()
        {
            if (!String.IsNullOrEmpty(GadgetProp.ArchivatorPath))
            {
                this.txtCabarcPath.Text = GadgetProp.ArchivatorPath;
            }
        }

        private void LoadSigntoolPath()
        {
            if (!String.IsNullOrEmpty(GadgetProp.SigntoolPath))
            {
                this.txtSigntoolPath.Text = GadgetProp.SigntoolPath;
            }
        }

        private void LoadPfxFilePath()
        {
            if (!String.IsNullOrEmpty(GadgetProp.PfxFilePath))
            {
                this.txtPfxFilePath.Text = GadgetProp.PfxFilePath;
            }
        }

        private void LoadPfxPwdPath()
        {
            if (!String.IsNullOrEmpty(GadgetProp.PfxPassword))
            {
                this.txtPrivateKeyPassword.Text = GadgetProp.PfxPassword;
            }
        }

        private void LoadAllowedFileExtensions()
        {
            if (!String.IsNullOrEmpty(GadgetProp.AllowedExtensionsForGadget))
            {
                this.txtAllowedFileExtension.Text = GadgetProp.AllowedExtensionsForGadget;
            }
        }

        private void chkIsBuildSignedGadget_CheckedChanged(object sender, EventArgs e)
        {
            this.pnlSignedGadgetBuildOptions.Enabled = this.chkIsBuildSignedGadget.Checked;
        }

        private void btnDefineOuputGadgetPath_Click(object sender, EventArgs e)
        {
            FilePathDeterminer fpd = new FilePathDeterminer("Define Vista Gadget Path", "newgadget.gadget", this.txtOutputGadgetPath.Text, FilePathDeterminer.FileOpenDialogType.FolderBrowserDialog);
            String FilePath = fpd.GetFilePath();
            SetValueToTextBox(this.txtOutputGadgetPath, FilePath);

        }

        private void btnDefineCabarcPath_Click(object sender, EventArgs e)
        {
            FilePathDeterminer fpd = new FilePathDeterminer("Define Archivator Path", "cabarc.exe", this.txtCabarcPath.Text, FilePathDeterminer.FileOpenDialogType.OpenFileDialog);
            string FilePath = fpd.GetFilePath();
            SetValueToTextBox(this.txtCabarcPath, FilePath);
        }

        private void btnDefineSigntoolPath_Click(object sender, EventArgs e)
        {
            FilePathDeterminer fpd = new FilePathDeterminer("Define signtool.exe Path", "signtool.exe", this.txtSigntoolPath.Text, FilePathDeterminer.FileOpenDialogType.OpenFileDialog);

            string FilePath = fpd.GetFilePath();
            SetValueToTextBox(this.txtSigntoolPath, FilePath);
        }

        private void btnDefinePfxFilePath_Click(object sender, EventArgs e)
        {
            FilePathDeterminer fpd = new FilePathDeterminer("Define .pfx Path", "*.pfx", this.txtPfxFilePath.Text, FilePathDeterminer.FileOpenDialogType.OpenFileDialog);
            string FilePath = fpd.GetFilePath();
            SetValueToTextBox(this.txtPfxFilePath, FilePath);
        }

        private void SetValueToTextBox(TextBox ctrl, string value)
        {
            if (!String.IsNullOrEmpty(value))
            {
                ctrl.Text = value;
            }
        }

        private Form CreateDialogForm(string message)
        {
            // Create a new instance of the form.
            Form form1 = new Form();

            // Create two buttons to use as the accept and cancel buttons.
            Button button1 = new Button();
            Button button2 = new Button();
            Label lblMessage = new Label();
            lblMessage.Text = message;
            lblMessage.Location = new Point(10, 10);
            lblMessage.Width = form1.ClientSize.Width - 20;
            lblMessage.TextAlign = ContentAlignment.MiddleCenter;
            lblMessage.Height = 100;
            // Set the text of button1 to "OK".
            button1.Text = "OK";
            //button1.Dock = DockStyle.Bottom;
            // Set the position of the button on the form.
            button1.Location = new Point(100, 170);
            // Set the text of button2 to "Cancel".
            button2.Text = "Cancel";
            //button2.Dock = DockStyle.Bottom;
            // Set the position of the button based on the location of button1.
            button2.Location = new Point(button1.Right + 20, button1.Top);
            // Set the caption bar text of the form.
            form1.Text = message;
            // Display a help button on the form.
            form1.HelpButton = true;

            // Define the border style of the form to a dialog box.
            form1.FormBorderStyle = FormBorderStyle.FixedDialog;
            // Set the accept button of the form to button1.
            form1.AcceptButton = button1;
            button1.DialogResult = System.Windows.Forms.DialogResult.OK;
            // Set the cancel button of the form to button2.
            form1.CancelButton = button2;
            button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            // Set the start position of the form to the center of the screen.
            form1.StartPosition = FormStartPosition.CenterScreen;

            form1.Controls.Add(lblMessage);
            // Add button1 to the form.
            form1.Controls.Add(button1);
            // Add button2 to the form.
            form1.Controls.Add(button2);

            return form1;
        }


    }
}
